<?php

namespace apiend\models;

use Yii;
use yii\data\ActiveDataProvider;

/**
 * This is the model class for table "{{%dynamic}}".
 *
 * @property int $id
 * @property string $user_id 用户ID
 * @property string $content
 * @property int $direction 动态类型：0主动动态，1被动动态
 * @property int $retweet_id 转发动态原始ID
 * @property int $status
 * @property string $created_at
 * @property string $updated_at
 * @property string $deleted_at
 */
class ArticleSearch extends \common\logics\TslineArticleLogic {

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $uid
     * @return ActiveDataProvider
     */
    public function search($params = []) {

        $query = static::find()->where([
            'status' => static::STATUS_ACTIVE,
            'deleted_at' => 0,
        ]);

        if (!empty($params)) {
            $query->andWhere($params);
        }

        $query->orderBy(['created_at' => SORT_DESC]);

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);

        Yii::$app->db->cache(function($db) use($dataProvider) {
            $dataProvider->prepare();
        }, 60);

        return $dataProvider;
    }

    public function getThumbs() {
        $thumbs = parent::getThumbs()->select('image')->column();
        array_walk($thumbs, function(&$val) {
            return $val = Yii::getAlias('@attachUrl') . $val;
        });
        return $thumbs;
    }

    public function fields() {
        $fields = parent::fields();
//        $this->__unset('content');
        return $fields;
    }

}
